package com.bestcem.xm.user.util.converter;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.user.controller.vo.user.CompanyVO;
import com.bestcem.xm.user.enums.BooleanStatusEnum;
import com.bestcem.xm.user.service.dto.user.CompanyDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 组织转换类·
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/15 16:02
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OrganizationConverter {

    /**
     * 公司信息转换
     *
     * @param companyDTO 公司信息
     * @return com.bestcem.xm.user.controller.vo.user.CompanyVO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/15 16:03
     */
    public static CompanyVO companyDTOtoVO(CompanyDTO companyDTO) {
        CompanyVO companyVO = new CompanyVO();
        if (Objects.nonNull(companyDTO)) {
            companyVO.setCode(companyDTO.getCode());
            companyVO.setCustomSetting(companyDTO.getCustomSetting());
            companyVO.setName(companyDTO.getName());
            companyVO.setStatus(companyDTO.getStatus());
            companyVO.setPackageExpireTime(companyDTO.getPackageExpireTime());
            companyVO.setFreeExpireTime(companyDTO.getFreeExpireTime());
            companyVO.setSkin(toJsonObject(companyDTO.getSkin()));
            companyVO.setPublicKey(companyDTO.getPublicKey());
            companyVO.setPackEdition(companyDTO.getPackEdition());
            companyVO.setCompleteFlag(BooleanStatusEnum.YES.getStatus().equals(companyDTO.getCompleteFlag()));
            companyVO.setEmail(companyDTO.getEmail());
            companyVO.setOrgName(companyDTO.getOrgName());
            companyVO.setContact(companyDTO.getContact());
            companyVO.setFooterEdit(companyDTO.getFooterEdit());
            companyVO.setIndustry(companyDTO.getIndustry());
            companyVO.setSettingsIsXm(companyDTO.getSettingsIsXm());
            companyVO.setSettingsNpShow(companyDTO.getSettingsNpShow());
            companyVO.setCreateTime(companyDTO.getCreateTime());
            companyVO.setPassConf(toJsonObject(companyDTO.getPassConf()));
            companyVO.setOrgId(companyDTO.getOrgId());
            companyVO.setUnNet(companyDTO.getUnNet());
            companyVO.setSource(companyDTO.getSource());
            companyVO.setContactsMobile(companyDTO.getContactsMobile());
            companyVO.setContactsEmail(companyDTO.getContactsEmail());
            companyVO.setContactsQrcode(companyDTO.getContactsQrcode());
            companyVO.setPlatformUrl(companyDTO.getPlatformUrl());
            companyVO.setPackPerm(companyDTO.getPackPerm());
            companyVO.setQuotainfo(companyDTO.getQuotainfo());
            companyVO.setPackOutName(companyDTO.getPackOutName());
            companyVO.setPackType(companyDTO.getPackType());
            companyVO.setPackAdDisplay(companyDTO.getPackAdDisplay());
            companyVO.setPackNavigationDisplay(companyDTO.getPackNavigationDisplay());
            companyVO.setUserReach(companyDTO.getUserReach());
            companyVO.setRespondentReach(companyDTO.getRespondentReach());
            companyVO.setProjectReach(companyDTO.getProjectReach());
            companyVO.setMemberIndReach(companyDTO.getMemberIndReach());
            companyVO.setTrialFlag(companyDTO.getTrialFlag());
            companyVO.setOpenMfa(companyDTO.getOpenMfa());
            companyVO.setOpenSpeechAnswer(companyDTO.getOpenSpeechAnswer());
        }

        return companyVO;
    }

    /**
     * 将字符串对象转成JSONObject
     *
     * @param jsonStr json字符串
     * @return com.alibaba.fastjson.JSONObject
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/15 16:11
     */
    private static JSONObject toJsonObject(String jsonStr) {
        if (StringUtils.isNotBlank(jsonStr)) {
            try {
                return com.alibaba.fastjson.JSON.parseObject(jsonStr);
            } catch (Exception e) {
                return new JSONObject();
            }
        }
        return new JSONObject();
    }

}
